package com.zhao.modleclass.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;
import com.zhao.modleclass.Helper.GlideImageLoader;
import com.zhao.modleclass.R;
import com.zhao.modleclass.activity.HPTagListActivity;
import com.zhao.modleclass.activity.HPType1Activity;
import com.zhao.modleclass.activity.Live_Fragment_Activity;
import com.zhao.modleclass.adapter.HP_LiveAdapter;
import com.zhao.modleclass.adapter.HP_NewAdapter;
import com.zhao.modleclass.adapter.HP_SelectedAdapter;
import com.zhao.modleclass.adapter.HP_TeacherLeft_Adapter;
import com.zhao.modleclass.adapter.HP_TeacherRight_Adapter;
import com.zhao.modleclass.bean.HomePageBean;
import com.zhao.modleclass.myview.MyLinearLayoutManager;
import com.zhao.modleclass.myview.NoScrollGridView;
import com.zhao.modleclass.myview.ToTopImageView;
import com.zhao.modleclass.presenter.Presenter_HomePage;
import com.zhao.modleclass.utils.SpacesItemDecoration;
import com.zhao.modleclass.view.IHomePageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 张昭 on 2016/11/25.
 */


public class HomePageFragment extends Fragment implements IHomePageView {
    @Bind(R.id.homePage_HP_banner)
    Banner homePageHPBanner;
    @Bind(R.id.iv_HP_taglist0)
    ImageView ivHPTaglist0;
    @Bind(R.id.tv_HP_taglist0)
    TextView tvHPTaglist0;
    @Bind(R.id.iv_HP_taglist1)
    ImageView ivHPTaglist1;
    @Bind(R.id.tv_HP_taglist1)
    TextView tvHPTaglist1;
    @Bind(R.id.iv_HP_taglist2)
    ImageView ivHPTaglist2;
    @Bind(R.id.tv_HP_taglist2)
    TextView tvHPTaglist2;
    @Bind(R.id.iv_HP_taglist3)
    ImageView ivHPTaglist3;
    @Bind(R.id.tv_HP_taglist3)
    TextView tvHPTaglist3;
    @Bind(R.id.iv_HP_taglist4)
    ImageView ivHPTaglist4;
    @Bind(R.id.tv_HP_taglist4)
    TextView tvHPTaglist4;

    @Bind(R.id.rv_Hp_Live)
    RecyclerView rvHpLive;

    @Bind(R.id.gv_HP_SELECT)
    NoScrollGridView gvHpSelected;
    @Bind(R.id.llllll)
    TextView llllll;
    @Bind(R.id.kkkkkk)
    TextView kkkkkk;
    @Bind(R.id.jjjjjj)
    TextView jjjjjj;
    @Bind(R.id.hhhhhh)
    TextView hhhhhh;
    //帧布局占位
    @Bind(R.id.fl_Hp_container)
    FrameLayout flHpContainer;
    //最新课程
    @Bind(R.id.rv_Hp_New)
    RecyclerView rvHpNew;
    @Bind(R.id.ll_tag0)
    LinearLayout llTag0;
    @Bind(R.id.ll_tag1)
    LinearLayout llTag1;
    @Bind(R.id.ll_tag2)
    LinearLayout llTag2;
    @Bind(R.id.ll_tag3)
    LinearLayout llTag3;
    @Bind(R.id.ll_tag4)
    LinearLayout llTag4;
    @Bind(R.id.PTRS_HP)
    PullToRefreshScrollView PTRSHP;
    @Bind(R.id.tv_hp_banner_tag0)
    TextView tvHpBannerTag0;
    @Bind(R.id.tv_hp_banner_tag1)
    TextView tvHpBannerTag1;
    @Bind(R.id.tv_hp_banner_tag2)
    TextView tvHpBannerTag2;
    @Bind(R.id.tv_hp_banner_tag3)
    TextView tvHpBannerTag3;
    @Bind(R.id.tv_hp_banner_tag4)
    TextView tvHpBannerTag4;
    @Bind(R.id.iv_HP_ToTop)
    ToTopImageView ivHPToTop;
    private List<TextView> mList_BannerTag;
    private Presenter_HomePage presenter;
    private int pageNo = 1;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new Presenter_HomePage(this);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_homepage, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ivHPToTop.setLimitHeight(800);
        mList_BannerTag = new ArrayList<>();
        mList_BannerTag.add(tvHpBannerTag0);
        mList_BannerTag.add(tvHpBannerTag1);
        mList_BannerTag.add(tvHpBannerTag2);
        mList_BannerTag.add(tvHpBannerTag3);
        mList_BannerTag.add(tvHpBannerTag4);
        //填充首页中ViewPager
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        HP_Fragment_Teacher fragment_teacher = new HP_Fragment_Teacher();
        if (!fragment_teacher.isAdded()) {
            transaction.add(R.id.fl_Hp_container, fragment_teacher);
            transaction.commit();
        }
        //布局中加粗
        TextView llllll, kkkkkk, jjjjjj, hhhhhh, zzzzzz, xxxxxx, cccccc, vvvvvv;
        llllll = (TextView) view.findViewById(R.id.llllll);
        kkkkkk = (TextView) view.findViewById(R.id.kkkkkk);
        jjjjjj = (TextView) view.findViewById(R.id.jjjjjj);
        hhhhhh = (TextView) view.findViewById(R.id.hhhhhh);
        zzzzzz = (TextView) view.findViewById(R.id.zzzzzz);
        xxxxxx = (TextView) view.findViewById(R.id.xxxxxx);
        cccccc = (TextView) view.findViewById(R.id.cccccc);
        vvvvvv = (TextView) view.findViewById(R.id.vvvvvv);
        TextPaint tp1 = llllll.getPaint();
        tp1.setFakeBoldText(true);
        TextPaint tp2 = kkkkkk.getPaint();
        tp2.setFakeBoldText(true);
        TextPaint tp3 = jjjjjj.getPaint();
        tp3.setFakeBoldText(true);
        TextPaint tp4 = hhhhhh.getPaint();
        tp4.setFakeBoldText(true);
        TextPaint tp5 = zzzzzz.getPaint();
        tp5.setFakeBoldText(true);
        TextPaint tp6 = xxxxxx.getPaint();
        tp6.setFakeBoldText(true);
        TextPaint tp7 = cccccc.getPaint();
        tp7.setFakeBoldText(true);
        TextPaint tp8 = vvvvvv.getPaint();
        tp8.setFakeBoldText(true);


        //初始化RecyclerView
        //使用重写的LinearLayoutManger解决滑动嵌套的冲突问题
        rvHpLive.setNestedScrollingEnabled(false);
        rvHpLive.setLayoutManager(new MyLinearLayoutManager(getActivity()));
        rvHpLive.addItemDecoration(new SpacesItemDecoration(2));

        rvHpNew.setNestedScrollingEnabled(false);
        rvHpNew.setLayoutManager(new MyLinearLayoutManager(getActivity()));
        rvHpNew.addItemDecoration(new SpacesItemDecoration(2));

        //传入要访问的字段值
        final Map<String, String> map = new HashMap<>();
        //deviceId=4024b89134ec3dd6818f09e422d2cafa&appVersion=120&pageSize=10&pageNo=1
        map.put("deviceId", "4024b89134ec3dd6818f09e422d2cafa");
        map.put("appVersion", "130");
        map.put("pageSize", "10");
        loadDataFromNet(map);
        //设置刷新为上拉和下拉
        PTRSHP.setMode(PullToRefreshBase.Mode.BOTH);
        PTRSHP.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            //下拉刷新时
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                pageNo = 1;
                loadDataFromNet(map);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                pageNo++;
                loadDataFromNet(map);
            }
        });
    }

    private void loadDataFromNet(Map<String, String> map) {
        map.put("pageNo", pageNo + "");
        presenter.LoadData(getActivity(), map, rvHpNew);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void toSetData(final HomePageBean homePageBean) {
        if (homePageBean != null) {
            List<String> list_ImagUrl = new ArrayList<>();
            for (int i = 0; i < homePageBean.getFocus().size(); i++) {
                list_ImagUrl.add(homePageBean.getFocus().get(i).getImageUrl());
            }
            homePageHPBanner.setBannerStyle(BannerConfig.CENTER);
            homePageHPBanner.setImageLoader(new GlideImageLoader());
            homePageHPBanner.setImages(list_ImagUrl);
            homePageHPBanner.isAutoPlay(true);
            homePageHPBanner.setDelayTime(3000);
            homePageHPBanner.setIndicatorGravity(BannerConfig.CENTER);
            mList_BannerTag.get(0).setVisibility(View.VISIBLE);
            homePageHPBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    for (int i = 0; i < mList_BannerTag.size(); i++) {
                        mList_BannerTag.get(i).setVisibility(View.INVISIBLE);
                    }
                    if (position > mList_BannerTag.size()) {
                        position = 1;
                    } else if (position < 1) {
                        position = mList_BannerTag.size();
                    }
                    mList_BannerTag.get(position - 1).setVisibility(View.VISIBLE);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            homePageHPBanner.setOnBannerClickListener(new OnBannerClickListener() {
                @Override
                public void OnBannerClick(int position) {
                    Intent intent = new Intent();
                    if (homePageBean.getFocus().get(position - 1).getTypeName().equals("videoCourseInfo")) {
                        //typeName=videoCourseInfo
                        intent.setClass(getActivity(), HPType1Activity.class);
                        intent.putExtra("Type", 1);
                        intent.putExtra("CourseId", homePageBean.getFocus().get(position - 1).getContentId());
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("banner", homePageBean.getFocus().get(position - 1));
                        intent.putExtra("banner", bundle);
                        startActivity(intent);

                        //typeName=liveCourseInfo
                    } else if (homePageBean.getFocus().get(position - 1).getTypeName().equals("liveCourseInfo")) {
                        intent.setClass(getActivity(), Live_Fragment_Activity.class);
                        intent.putExtra("CourseId", homePageBean.getFocus().get(position - 1).getContentId());
                        startActivity(intent);
                    }

                }
            });

            homePageHPBanner.start();
            //tagList 添加数据
            Glide.with(getActivity()).load(homePageBean.getTagList().get(0).getTagImageUrl()).into(ivHPTaglist0);
            tvHPTaglist0.setText(homePageBean.getTagList().get(0).getTagName());
            Glide.with(getActivity()).load(homePageBean.getTagList().get(1).getTagImageUrl()).into(ivHPTaglist1);
            tvHPTaglist1.setText(homePageBean.getTagList().get(1).getTagName());
            Glide.with(getActivity()).load(homePageBean.getTagList().get(2).getTagImageUrl()).into(ivHPTaglist2);
            tvHPTaglist2.setText(homePageBean.getTagList().get(2).getTagName());
            Glide.with(getActivity()).load(homePageBean.getTagList().get(3).getTagImageUrl()).into(ivHPTaglist3);
            tvHPTaglist3.setText(homePageBean.getTagList().get(3).getTagName());
            Glide.with(getActivity()).load(homePageBean.getTagList().get(4).getTagImageUrl()).into(ivHPTaglist4);
            tvHPTaglist4.setText(homePageBean.getTagList().get(4).getTagName());
            //为LinearLayout添加监听
            setllListener(homePageBean);

        }
    }

    private void setllListener(final HomePageBean homePageBean) {
        llTag0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HPTagListActivity.class);
                intent.putExtra("tagId", homePageBean.getTagList().get(0).getTagId());
                intent.putExtra("tagName", homePageBean.getTagList().get(0).getTagName());
                startActivity(intent);
            }
        });
        llTag1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HPTagListActivity.class);
                intent.putExtra("tagId", homePageBean.getTagList().get(1).getTagId());
                intent.putExtra("tagName", homePageBean.getTagList().get(1).getTagName());
                startActivity(intent);
            }
        });
        llTag2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HPTagListActivity.class);
                intent.putExtra("tagId", homePageBean.getTagList().get(2).getTagId());
                intent.putExtra("tagName", homePageBean.getTagList().get(2).getTagName());
                startActivity(intent);
            }
        });
        llTag3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HPTagListActivity.class);
                intent.putExtra("tagId", homePageBean.getTagList().get(3).getTagId());
                intent.putExtra("tagName", homePageBean.getTagList().get(3).getTagName());
                startActivity(intent);
            }
        });
        llTag4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HPTagListActivity.class);
                intent.putExtra("tagId", homePageBean.getTagList().get(4).getTagId());
                intent.putExtra("tagName", homePageBean.getTagList().get(4).getTagName());
                startActivity(intent);
            }
        });
    }

    @Override
    public void toSetLive(HP_LiveAdapter adapter) {
        rvHpLive.setAdapter(adapter);
    }

    @Override
    public void toSetSelected(HP_SelectedAdapter adapter) {
        gvHpSelected.setAdapter(adapter);
    }

    @Override
    public void toSetTeacherLeft(HP_TeacherLeft_Adapter adapter) {
        HP_Teacher_Left.gridLeftView.setAdapter(adapter);
    }

    @Override
    public void toSetTeacherRight(HP_TeacherRight_Adapter adapter) {
        HP_Teacher_Right.gridRightView.setAdapter(adapter);
    }

    @Override
    public void toSetNew(HP_NewAdapter adapter) {
        ivHPToTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                final ScrollView scrollView = PTRSHP.getRefreshableView();
                //mScrollView.setOnTouchListener();  无效
                scrollView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()){
                            case MotionEvent.ACTION_MOVE:
                                ivHPToTop.setVisibility(View.VISIBLE);
                                ivHPToTop.tellMe(scrollView);
                                break;
                        }
                        return false;
                    }
                });
//                ivHPToTop.setVisibility(View.INVISIBLE);
            }
        });


        if (pageNo == 1) {
            rvHpNew.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        PTRSHP.onRefreshComplete();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        ivHPToTop.clearCallBacks();
    }

}
